package com.wuchao.servlet143;

import com.wuchao.entity.Pojo;
import com.wuchao.entity.Product;
import com.wuchao.service.serviceimpl.ProductImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * @author 吴超66
 * @version 1.0
 * @date 2022/4/26 14:31
 */
@WebServlet(name = "ServletNC",urlPatterns = "/ServletNC")
public class ServletNC extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();//文件流
        out.println();

        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //一定会乱码
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();//文件流
        out.println();
        ProductImpl product = new ProductImpl();
        String op =request.getParameter("op");
        if(op!=null){
            if(op.equals("delete")){
                int id=Integer.parseInt(request.getParameter("id"));
                int update = product.update("delete from product where id=?", id);
                if(update>0){
                    List<Product> poJo = product.findPoJo("select * from product");
                    request.setAttribute("list",poJo);
                    request.setAttribute("delete1","删除成功");
                    request.getRequestDispatcher("demonc/main1.jsp").forward(request,response);
                }else{
                    List<Product> poJo = product.findPoJo("select * from product");
                    request.setAttribute("list",poJo);
                    request.setAttribute("delete2","删除失败");
                   // response.sendRedirect(request.getContextPath()+"/demonc/main1.jsp");
                    request.getRequestDispatcher("demonc/main1.jsp").forward(request,response);
                }

            }else  if(op.equals("delete1")){
                int id=Integer.parseInt(request.getParameter("id"));
                int update = product.update("delete from product where id=?", id);
                if(update>0){
                    out.println("y");
                }else{
                    out.println("n");
                }

            } else if(op.equals("add")){
                String name=request.getParameter("name");
                String sex=request.getParameter("select");
                int flage=Integer.parseInt(request.getParameter("radio"));
                int update = product.update("insert product(name,sex,flage) values(?,?,?)", name,sex,flage);
                if(update>0){
                    List<Product> poJo = product.findPoJo("select * from product");
                    request.setAttribute("list",poJo);
                    request.setAttribute("add1","增加成功");
                    request.getRequestDispatcher("demonc/main1.jsp").forward(request,response);
                }else{
                    List<Product> poJo = product.findPoJo("select * from product");
                    request.setAttribute("list",poJo);
                    request.setAttribute("add2","增加失败");
                    // response.sendRedirect(request.getContextPath()+"/demonc/main1.jsp");
                    request.getRequestDispatcher("demonc/main1.jsp").forward(request,response);
                }
            }else if(op.equals("update1")){
                int id=Integer.parseInt(request.getParameter("id"));
                List<Product> poJo1 = product.findPoJo("select * from product where id=?", id);
                if(poJo1.size()>0){
                    request.setAttribute("list",poJo1);
                    request.getRequestDispatcher("demonc/update.jsp").forward(request,response);
                }else{
                    List<Product> poJo = product.findPoJo("select * from product");
                    request.setAttribute("list",poJo);
                    request.setAttribute("update11","无法修改因为没找到参数");
                    // response.sendRedirect(request.getContextPath()+"/demonc/main1.jsp");
                    request.getRequestDispatcher("demonc/main1.jsp").forward(request,response);
                }

            }else if(op.equals("update2")){
                int id=Integer.parseInt(request.getParameter("id"));
                String name=request.getParameter("name");
                String sex=request.getParameter("select");
                int flage=Integer.parseInt(request.getParameter("radio"));
                int update = product.update("update product set name=?,sex=?,flage=? where id=?", name,sex,flage,id);
                if(update>0){
                    List<Product> poJo = product.findPoJo("select * from product");
                    request.setAttribute("list",poJo);
                    request.setAttribute("update1","修改成功");
                    request.getRequestDispatcher("demonc/main1.jsp").forward(request,response);
                }else{
                    List<Product> poJo = product.findPoJo("select * from product");
                    request.setAttribute("list",poJo);
                    request.setAttribute("update2","修改失败");
                    request.getRequestDispatcher("demonc/main1.jsp").forward(request,response);
                }
            }
            else{
                List<Product> poJo = product.findPoJo("select * from product");
                request.setAttribute("list",poJo);
                response.sendRedirect(request.getContextPath()+"/demonc/main1.jsp");
            }

        }else{//首次直接带值去main
            List<Product> poJo = product.findPoJo("select * from product");
            request.setAttribute("list",poJo);
            request.getRequestDispatcher("demonc/main1.jsp").forward(request,response);

        }


        out.flush();
        out.close();
    }
}
